<?php

namespace App\Http\Controllers\Test;


use App\Http\Controllers\Controller;
use App\Logics\TagCacheLogic;
use App\Service\BangCache;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Context;

class TagCacheController extends Controller
{


    // 模型缓存


    //  查询测试
    // http://www.light.com/test/tag-cache/one
    public function one()
    {
        DB::connection()->enableQueryLog();

        $id   = 1;
        $key  = 'id:'.$id;
        $info = DB::table('article')->where('id', $id)->first();
        $data = Cache::get($key);

        $this->showMsg('获取缓存的数据', $data);
        $this->showMsg('获取查询语句', DB::getQueryLog());
        $this->showMsg('获取查询得到的数据', $info);
    }


    //  缓存测试
    // http://www.light.com/test/tag-cache/cache
    public function cache()
    {
        $logic = new TagCacheLogic();
        $logic->cache();
    }



    //  查询详情
    //    实现成功：不论有没有数据都生成缓存，删除绑定后，重新查询
    // http://www.light.com/test/tag-cache/find
    public function find()
    {
        DB::connection()->enableQueryLog();
        $logic = new TagCacheLogic();

        $id    = 10000;
        $id    = 1;
        $where = ['id' => $id];
        $table = 'article';

        // 查询
        $info = $logic->find($table, $where);
        $this->showMsg('第一次获取缓存的数据', $info);
        $this->showMsg('第一次获取查询语句', DB::getQueryLog());

        // 删除缓存
        $bang = new BangCache();
        $tags = $bang->makeBang($table, $where);   // where条件制作绑定
        $bang->deleteTag($tags);

        $info = $logic->find($table, $where);
        $this->showMsg('删除绑定缓存后：获取缓存的数据', $info);
        $this->showMsg('删除绑定缓存后：获取查询语句', DB::getQueryLog());

    }


    //  查询列表
    //    实现成功：不论有没有数据都生成缓存，删除绑定后，重新查询
    // http://www.light.com/test/tag-cache/list
    public function list()
    {
        DB::connection()->enableQueryLog();
        $logic = new TagCacheLogic();


        $table = 'article';
        $where = ['site_id' => 1];
        $where = ['site_id' => 20];  // 不存在的情况
        $field = '*';
        $page  = 1;
        $rows  = 20;
        $order = 'id ASC';


        // 查询
        $list = $logic->lists($table, $where, $field, $page, $rows, $order);
        //$this->showMsg('第一次获取缓存的数据', $list);
        $this->showMsg('第一次获取查询语句', DB::getQueryLog());

        // 删除缓存
        $bang = new BangCache();
        $tags = $bang->makeBang($table, $where);   // where条件制作绑定
        $bang->deleteTag($tags);

        $list = $logic->lists($table, $where, $field, $page, $rows, $order);
        //$this->showMsg('删除绑定缓存后：获取缓存的数据', $list);
        $this->showMsg('删除绑定缓存后：获取查询语句', DB::getQueryLog());
    }

    ############################################################################################

    // 输出
    protected function showMsg($msg, $var)
    {
        echo '<h5>'.$msg.'：</h5>';
        if($var){
            if(is_string($var)){
                echo $var;
            } else{
                var_dump($var);
            }

            echo '<br><br>';
        }
    }

}


